import http from '@/http';
import { IModel, IModelScore, IState, IUserInfo } from '@/types/app';
import Vue from 'vue';
import Vuex from 'vuex';
import app from './modules/app';
import qs from 'qs';
import createPersistedState from 'vuex-persistedstate';

Vue.use(Vuex);

const store = new Vuex.Store<IState>({
	state: {
		models: [
			{ id: 1, model: '杭州市西湖区时空猎人模型', addr: '杭州市西湖区公安局' },
			{ id: 2, model: '杭州市江干区鬼猎人模型', addr: '杭州市江干区公安局' },
			{ id: 3, model: '杭州市上城区小蜘蛛模型', addr: '杭州市上城区公安局' },
			{ id: 4, model: '杭州市下城区猎人模型', addr: '杭州市下城区公安局' },
			{ id: 5, model: '杭州市拱墅区时空猎人模型', addr: '杭州市拱墅区公安局' },
			{ id: 6, model: '杭州市滨江区时空猎人模型', addr: '杭州市滨江区公安局' }
		],
		user: {
			compeId: '',
			companyName: '',
			competitionName: '',
			judgeName: '',
			judgeId: ''
		},
		model: {
			score: null,
			item: {
				itemId: '',
				name: '',
				content: '',
				company: '',
				companyPerson: '',
				totalScore: 0,
				finalScore: 0,
				remarks: '',
				createUser: '',
				createTime: 0,
				updateUser: '',
				updateTime: 0,
				delFlag: '',
				dealFlag: '',
				competitionName: '',
				itemCode: ''
			}
		},
		modelScore: {
			minScore: '0',
			maxScore: '0'
		}
	},
	mutations: {
		setUserInfo(state, payload) {
			state.user = payload;
		},
		setModel(state, payload) {
			state.model = payload;
		},
		setScoreLimit(state, payload) {
			state.modelScore = payload;
		},
		logout(state) {
			state.model = {
				score: '',
				item: {
					itemId: '',
					name: '',
					content: '',
					company: '',
					companyPerson: '',
					totalScore: 0,
					finalScore: 0,
					remarks: '',
					createUser: '',
					createTime: 0,
					updateUser: '',
					updateTime: 0,
					delFlag: '',
					dealFlag: '',
					competitionName: '',
					itemCode: ''
				}
			};
			state.modelScore = {
				minScore: '',
				maxScore: ''
			};
			state.user = {
				competitionName: '',
				companyName: '',
				compeId: '',
				judgeName: '',
				judgeId: ''
			};
		}
	},
	actions: {
		async login({ state, commit, dispatch }, payload) {
			return new Promise((resolve, reject) => {
				http
					.post<IUserInfo>('/login/checkJudgesBynameAnddept?' + qs.stringify(payload))
					.then(async (res) => {
						commit('setUserInfo', res.data);
						try {
							await dispatch('getItem', {
								judgesId: res.data.judgeId,
								compeId: res.data.compeId
							});
						} catch (error) {
							reject(error);
						}
						try {
							await dispatch('getScoreLimit');
						} catch (error) {
							reject(error);
						}
						resolve();
					})
					.catch((err) => reject(err));
			});
		},
		async getItem({ state, commit, dispatch }, payload) {
			return new Promise((resolve, reject) => {
				http
					.post<IModel>('/login/getScoringItemAndScore?' + qs.stringify(payload))
					.then((res) => {
						commit('setModel', res.data);
						if (res.data.item == null) {
							reject('暂无进行中的项目');
							return;
						}
						resolve();
					})
					.catch((err) => {
						reject(err);
					});
			});
		},
		async getScoreLimit({ commit }, payload) {
			return new Promise((resolve, reject) => {
				http
					.post<IModelScore>('/login/getScoreLimit')
					.then((res) => {
						commit('setScoreLimit', res.data);
						resolve();
					})
					.catch((err) => {
						reject(err);
					});
			});
		}
	},
	plugins: [createPersistedState()]
});

export default store;
